package com.winteam.View;



import com.winteam.Bean.CheckOut;
import com.winteam.Controler.Select;
import com.winteam.Controler.Updata;



import javax.swing.*;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class CheckOutDemo extends JFrame {

	private JPanel Pane;
	private JTextField moneyField;
	private JTextField payField;
	Updata update = new Updata();
	JLabel oddnumsLabel,oddroomLabel,nameLabel,moneyLabel,oddnumsNewLabel,
			roomid ,name,money,lblBs_1_1,oddnumsLabel_1_1_1,lblBs_1_1_1,lblBs_1_1_1_1_1;
	private JTextField remarksField;

		Color bg = new Color(231,215,183);
	/**
	 * Create the frame.
	 */
	CheckOut checkOut;
	String admin;
	public CheckOutDemo(CheckOut checkOut,String admin) {
		this.checkOut =checkOut;
		this.admin =admin;
		setDefaultCloseOperation(1);
		this.setTitle("收银结账");
		this.setSize(767,258);
		this.setLocationRelativeTo(null);
		Pane = new JPanel();
		Pane.setBackground(bg);//背景颜色
		Pane.setLayout(null);
		setContentPane(Pane);
		

		
		 oddnumsLabel = new JLabel("结账单号:");
		oddnumsLabel.setFont(new Font("宋体", Font.BOLD, 18));
		oddnumsLabel.setBounds(23, 20, 97, 28);
		Pane.add(oddnumsLabel);
		
		 oddroomLabel = new JLabel("结账房间:");
		oddroomLabel.setFont(new Font("宋体", Font.BOLD, 18));
		oddroomLabel.setBounds(330, 20, 97, 28);
		Pane.add(oddroomLabel);
		
		 nameLabel = new JLabel("宾客姓名:");
		nameLabel.setFont(new Font("宋体", Font.BOLD, 18));
		nameLabel.setBounds(564, 20, 97, 28);
		Pane.add(nameLabel);
		
		 moneyLabel = new JLabel("实收金额:");
		moneyLabel.setFont(new Font("宋体", Font.BOLD, 14));
		moneyLabel.setBounds(23, 113, 78, 28);
		Pane.add(moneyLabel);
		
		moneyField = new JTextField();//结账单号
		moneyField.setBounds(104, 115, 138, 25);
		Pane.add(moneyField);
		moneyField.setColumns(10);
		
		JLabel payLabel = new JLabel("宾客支付:");
		payLabel.setFont(new Font("宋体", Font.BOLD, 14));
		payLabel.setBounds(275, 113, 78, 28);
		Pane.add(payLabel);
		
		payField = new JTextField();//宾客支付
		payField.setColumns(10);
		payField.setBounds(351, 115, 138, 25);
		Pane.add(payField);
		
		JLabel remarksLabel = new JLabel("结账备注:");
		remarksLabel.setFont(new Font("宋体", Font.BOLD, 14));
		remarksLabel.setBounds(23, 159, 78, 28);
		Pane.add(remarksLabel);
		
		remarksField = new JTextField();
		remarksField.setBounds(104, 161, 252, 25);
		Pane.add(remarksField);
		remarksField.setColumns(10);
		
		JButton paymentbtn = new JButton("结 账",new ImageIcon(getClass().getResource("img/u04.gif")));

		paymentbtn.setFont(new Font("宋体", Font.BOLD, 16));
		paymentbtn.setBounds(434, 156, 97, 33);
		Pane.add(paymentbtn);
		
		JButton cancelbtn = new JButton("取 消",new ImageIcon(getClass().getResource("img/cancel.gif")));
		cancelbtn.setFont(new Font("宋体", Font.BOLD, 16));
		cancelbtn.setBounds(579, 156, 97, 33);
		Pane.add(cancelbtn);
		
		JLabel cashierDeskLabel = new JLabel("结账区内房间消费清单");
		cashierDeskLabel.setFont(new Font("宋体", Font.BOLD, 17));
		cashierDeskLabel.setBounds(252, 226, 197, 28);
		Pane.add(cashierDeskLabel);

		 oddnumsNewLabel = new JLabel("YG201408140001");//结账单号label
		oddnumsNewLabel.setFont(new Font("宋体", Font.BOLD, 20));
		oddnumsNewLabel.setForeground(Color.BLUE);
		oddnumsNewLabel.setBounds(114, 14, 189, 40);
		Pane.add(oddnumsNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("找     零:");
		lblNewLabel_1.setForeground(Color.BLUE);
		lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 18));
		lblNewLabel_1.setBounds(515, 106, 105, 40);
		Pane.add(lblNewLabel_1);

		 roomid = new JLabel("BS2001");//结账房间
		roomid.setForeground(Color.BLUE);
		roomid.setFont(new Font("宋体", Font.BOLD, 20));
		roomid.setBounds(430, 13, 87, 40);
		Pane.add(roomid);

		 name = new JLabel("fsaafs");//宾客姓名
		name.setForeground(Color.BLUE);
		name.setFont(new Font("宋体", Font.BOLD, 20));
		name.setBounds(657, 13, 110, 40);
		Pane.add(name);
		
		JLabel paymoneyLabel = new JLabel("消费金额:");
		paymoneyLabel.setForeground(Color.BLUE);
		paymoneyLabel.setFont(new Font("宋体", Font.BOLD, 18));
		paymoneyLabel.setBounds(23, 63, 97, 28);
		Pane.add(paymoneyLabel);

		  money = new JLabel("100.0");//消费金额
		money.setForeground(Color.BLUE);
		money.setFont(new Font("宋体", Font.BOLD, 20));
		money.setBounds(124, 56, 67, 40);
		Pane.add(money);
		
		JLabel oddnumsLabel_1_1 = new JLabel("应收金额:");
		oddnumsLabel_1_1.setForeground(Color.BLUE);
		oddnumsLabel_1_1.setFont(new Font("宋体", Font.BOLD, 18));
		oddnumsLabel_1_1.setBounds(201, 63, 97, 28);
		Pane.add(oddnumsLabel_1_1);
		
		 lblBs_1_1 = new JLabel("100.0");
		lblBs_1_1.setForeground(Color.BLUE);
		lblBs_1_1.setFont(new Font("宋体", Font.BOLD, 20));
		lblBs_1_1.setBounds(300, 56, 67, 40);
		Pane.add(lblBs_1_1);
		
		 oddnumsLabel_1_1_1 = new JLabel("已收押金:");
		oddnumsLabel_1_1_1.setForeground(Color.BLUE);
		oddnumsLabel_1_1_1.setFont(new Font("宋体", Font.BOLD, 18));
		oddnumsLabel_1_1_1.setBounds(389, 63, 97, 28);
		Pane.add(oddnumsLabel_1_1_1);

		 lblBs_1_1_1 = new JLabel("100.0");
		lblBs_1_1_1.setForeground(Color.RED);
		lblBs_1_1_1.setFont(new Font("宋体", Font.BOLD, 20));
		lblBs_1_1_1.setBounds(487, 56, 67, 40);
		Pane.add(lblBs_1_1_1);

		lblBs_1_1_1_1_1 = new JLabel("0.0");
		lblBs_1_1_1_1_1.setForeground(Color.RED);
		lblBs_1_1_1_1_1.setFont(new Font("宋体", Font.BOLD, 20));
		lblBs_1_1_1_1_1.setBounds(630, 105, 67, 40);
		Pane.add(lblBs_1_1_1_1_1);
		paymentbtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (moneyField.getText().equals("")){
					JOptionPane.showMessageDialog(null,"请先输入实收金额");
					return;
				}
				if (addCheck()){
					int userid =new Select().findClientId(name.getText());//用户的id

					int roomidss =new Select().findRoomId(checkOut.getR_rommber());//房间的id
					if (update.live(new Select().findClientId(userid,roomidss))) {
						System.out.println("用户状态改变完毕");
						if (update.changeRoomStaate(checkOut.getR_rommber())) {
							System.out.println("房间状态改变完毕");
							JOptionPane.showMessageDialog(null,"结账完成");
							dispose();
						}
					}

				}

			}
		});
		cancelbtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
					dispose();
			}
		});

		paymentbtn.setOpaque(false);
		cancelbtn.setOpaque(false);
		cancelbtn.setBackground(new Color(231,215,183));
		paymentbtn.setBackground(new Color(231,215,183));
		data(checkOut);
		verify();
		this.setVisible(true);
	}



	private void data( CheckOut checkOut){
		oddnumsNewLabel.setText("DRNM"+System.currentTimeMillis());//单号
		roomid.setText(checkOut.getR_rommber());//房价id
		name.setText(checkOut.getC_name());//宾客姓名
		money.setText((checkOut.getR_price()*checkOut.getDay())+"");//消费金额
		lblBs_1_1.setText((checkOut.getR_price()*checkOut.getDay())+"");//应收押金
		lblBs_1_1_1.setText(checkOut.getC_capr());//已收押金
	}

	/**
	 *
	 */
	public void verify(){
		payField.addFocusListener(new FocusListener(){
			@Override
			public void focusGained(FocusEvent e) {

			}
			@Override
			public void focusLost(FocusEvent e) {
				//失去焦点执行的代码
				/**
				 * 先获取到要实际付钱的数量
				 * 把获取的到字符串转换成int
				 */
				if (moneyField.getText().equals("")){
					JOptionPane.showMessageDialog(null,"请先输入实收金额");
					return;
				}
				int money = Integer.parseInt(moneyField.getText());
				int pay=Integer.parseInt(payField.getText());
				if(pay<money){
					JOptionPane.showMessageDialog(null,"你付的前还不够哦");
					return;
				}else if(pay>money){

					int zl = money-pay;

					if (Math.abs(zl)<=0){
						zl=0;
					}
					lblBs_1_1_1_1_1.setText(Math.abs(zl)+"");
				}
			}
		});

	}
	private boolean addCheck() {
	;//订单号
		int clientId = new Select().findClientId(checkOut.getC_name(),checkOut.getR_rommber());

		if(update.addChecOut(oddnumsNewLabel.getText(),clientId,checkOut.getDay(),Integer.parseInt(payField.getText()),admin,remarksField.getText())){
			System.out.println("订单信息生产成功");
			/**
			 * 通过用户名得到用户id 修改用户状态
			 */

			return true;//如果修改成功
		}
		return false;
	}

}
